package com.yhd.controller;

import com.yhd.entity.dto.CourseBookedDto;
import com.yhd.entity.dto.CourseDto;
import com.yhd.entity.dto.UserDto;
import com.yhd.entity.query.CourseBookedVO;
import com.yhd.entity.query.UserVO;
import com.yhd.entity.result.ResultVo;
import com.yhd.model.ResultPage;
import com.yhd.service.CourseService;
import com.yhd.service.UserService;
import com.yhd.utils.AesUtils;
import com.yhd.utils.RedisUtils;
import com.yhd.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(value = "用户管理")
@Slf4j
@RequestMapping(value = "/user")
@CrossOrigin
public class UserController extends WebBaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "登录", httpMethod = "POST", notes = "{\n" +
            "  \"loginId\": \"登录名\",\n" +
            "  \"password\": \"登录密码\",\n" +
            "  \"type\": \"类型 customer:客户 teacher:老师  system:系统平台\"\n" +
            "}")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name = "loginId", value = "登录账号", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", paramType = "query", required = true, dataType = "string"),
            @ApiImplicitParam(name = "type", value = "类型 customer:客户 teacher:老师  system:系统平台", paramType = "query", required = true, dataType = "string"),
    })*/
    @PostMapping("/login")
    @ResponseBody
    public ResultVo login(@RequestBody UserVO userVO) {
        try {
            userVO.setPassword(AesUtils.encrypt(AesUtils.DEFAULT_PWS,userVO.getPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        UserDto userDto = userService.login(userVO);
        if (null != userDto){
            String token = TokenUtils.token(userVO.getLoginId(),userVO.getPassword());
            redisUtils.set(userVO.getType() + "-"  + token, userDto.getId(),604800L);
            userDto.setAuthToken(token);
            return ResultVo.success(userDto);
        }
        return ResultVo.failure("10001","登录失败。");
    }
    
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "userName", value = "用户名称", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "inviter", value = "邀请人", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "referrer", value = "推荐人", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "phone", value = "电话", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "type", value = "类型 customer:客户 teacher:老师  system:系统平台", required = true, paramType = "int"),
            @ApiImplicitParam(name = "role", value = "角色：1=销售，2=教员，3=班主任，4=其他", required = true, paramType = "int"),
            @ApiImplicitParam(name = "registerStartTime", value = "注册开始时间", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "registerEndTime", value = "注册结束时间", required = false, paramType = "字符串"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, paramType = "int")})
    @GetMapping("/list")
    @ResponseBody
    public ResultVo<ResultPage<List<UserDto>>> list(UserVO userVO){
        ResultPage<List<UserDto>> resultPage = userService.selectList(userVO);
        return ResultVo.success(resultPage);
    }
    
    
    @ApiOperation(value = "删除用户", httpMethod = "DELETE", notes = "{\n" +
            "  \"id\": \"用户id\"\n" +
            "}")
    @DeleteMapping("/delete")
    @ResponseBody
    public ResultVo delete(@RequestBody UserVO userVO) {
        userService.deleteUser(userVO);
        return ResultVo.success();
    }
    
    @ApiOperation(value = "新增/编辑", httpMethod = "POST", notes = "{\n" +
            "  \"id\": \"用户id，新增不传，编辑必传\",\n" +
            "  \"userName\": \"用户名\",\n" +
            "  \"sex\": \"性别 0女 1\",\n" +
            "  \"phone\": \"电话\",\n" +
            "  \"cardNo\": \"证件号码\",\n" +
            "  \"invitedCode\": \"被邀请码\",\n" +
            "  \"freeNum\": \"体验次数\",\n" +
            "  \"type\": \"类型 customer:客户 teacher:老师  system:系统平台，用户列表新增=customer，员工列表新增=system\",\n" +
            "  \"role\": \"角色：0=默认值，1=销售，2=教员，3=班主任，4=其他\",\n" +
            "  \"courseId\": \"课程id\"\n" +
            "}")
    @PostMapping("/save")
    @ResponseBody
    public ResultVo<UserDto> save(@RequestBody UserVO userVO) {
        ResultVo<UserDto> resultVo = userService.saveUser(userVO);
        if (null != resultVo){
            return resultVo;
        }
        return ResultVo.failure("10003","操作失败。");
    }


    @ApiOperation(value = "用户详情", httpMethod = "GET",notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "用户Id", required = false, paramType = "字符串")})
    @GetMapping("/userInfo")
    @ResponseBody
    public ResultVo<UserDto> userInfo(@RequestParam String id) {
        UserDto result = userService.selectUserById(id);
        return ResultVo.success(result);
    }

    @ApiOperation(value = "用户活动/课程订单", httpMethod = "GET", notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户Id", required = true, paramType = "字符串"),
            @ApiImplicitParam(name = "type", value = "类型 activity:活动，course:课程", required = true, paramType = "字符串"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, paramType = "int")})
    @GetMapping("/order")
    @ResponseBody
    public ResultVo<ResultPage<List<CourseBookedDto>>>  courseOrder(@RequestParam String userId, String type, Integer pageSize, Integer pageNum) {
        ResultPage<List<CourseBookedDto>> result = courseService.userOrderList(type, null, userId, pageSize, pageNum);
        return ResultVo.success(result);
    }


    @ApiOperation(value = "收藏活动/课程", httpMethod = "GET", notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户Id", required = true, paramType = "字符串"),
            @ApiImplicitParam(name = "type", value = "类型 activity:活动，course:课程", required = true, paramType = "字符串")})
    @GetMapping("/collected")
    @ResponseBody
    public ResultVo<List<CourseDto>>  collected(@RequestParam String userId, String type) {
        List<CourseDto> result = courseService.userCollectedList(type, userId);
        return ResultVo.success(result);
    }

    @ApiOperation(value = "订单审核", httpMethod = "POST", notes = "{\n" +
            "  \"id\": \"订单id\",\n" +
            "  \"status\": \"1:通过 3:拒绝\"\n" +
            "}")
    @PostMapping("/orderAudit")
    @ResponseBody
    public ResultVo<CourseBookedDto> orderAudit(@RequestBody CourseBookedVO bookedVO) {
        CourseBookedDto result = courseService.orderAudit(bookedVO, getUserInfo(request));
        return ResultVo.success(result);
    }
}
